
package com.cqu.hospitalsystem.controller;

import com.alibaba.fastjson.JSONObject;
import com.cqu.hospitalsystem.Vo.preCheck;
import com.cqu.hospitalsystem.entity.Checkitem;
import com.cqu.hospitalsystem.service.CheckitemService;
import com.cqu.hospitalsystem.utils.result.DataResult;
import com.cqu.hospitalsystem.utils.result.code.Code;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("checkitem")
@Tag(name = "CheckitemController", description = "检查项管理")
public class CheckitemController {
    /**
     * 服务对象
     */
    @Resource
    private CheckitemService checkitemService;

    @PostMapping("cancelCheck")
    @Operation(summary = "取消检查", description = "根据预检查对象取消检查项")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "取消成功"),
            @ApiResponse(responseCode = "400", description = "取消失败")
    })
    public DataResult cancelCheck(@RequestBody @Parameter(description = "预检查对象") preCheck preCheck){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("isSuccess",this.checkitemService.cancelCheck(preCheck));
        return DataResult.successByData(jsonObject);
    }

    @PostMapping("allCheckitem")
    @Operation(summary = "获取所有检查项", description = "查询所有检查项")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到检查项")
    })
    public DataResult allCheckitem(){
        List<Checkitem> list=this.checkitemService.allCheckitem();
        JSONObject jsonObject = new JSONObject();
        if(list != null){
            jsonObject.put("checkitemList",list);
            DataResult dataResult=DataResult.successByData(jsonObject);
            dataResult.setcode(666);
            return dataResult;
        }else{
            return DataResult.errByErrCode(Code.ACCOUNT_ERROR);
        }
    }

    @PostMapping("chooseCheckitem")
    @Operation(summary = "选择检查项", description = "根据检查项对象选择检查项")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "选择成功"),
            @ApiResponse(responseCode = "404", description = "检查项不存在")
    })
    public DataResult chooseCheckitem(@RequestBody @Parameter(description = "检查项对象") Checkitem checkitem){
        System.out.println("chooseCheckitem");
        System.out.println(checkitem.getCheckitemId());
        Checkitem c=this.checkitemService.queryById(checkitem.getCheckitemId());
        if(c != null){
            DataResult dataResult=DataResult.successByData(c);
            dataResult.setcode(666);
            return dataResult;
        }else{
            return DataResult.errByErrCode(Code.ACCOUNT_ERROR);
        }
    }

    /**
     * 分页查询
     *
     * @param checkitem 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    @Operation(summary = "分页查询检查项", description = "根据筛选条件和分页信息查询检查项")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "400", description = "查询失败")
    })
    public ResponseEntity<Page<Checkitem>> queryByPage(Checkitem checkitem, PageRequest pageRequest) {
        return ResponseEntity.ok(this.checkitemService.queryByPage(checkitem, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @Operation(summary = "根据ID查询检查项", description = "根据检查项ID查询单个检查项")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "检查项不存在")
    })
    public ResponseEntity<Checkitem> queryById(@PathVariable("id") @Parameter(description = "检查项ID") Long id) {
        return ResponseEntity.ok(this.checkitemService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param checkitem 实体
     * @return 新增结果
     */
    @PostMapping
    @Operation(summary = "新增检查项", description = "创建新的检查项")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "201", description = "创建成功"),
            @ApiResponse(responseCode = "400", description = "创建失败")
    })
    public ResponseEntity<Checkitem> add(@RequestBody @Parameter(description = "检查项对象") Checkitem checkitem) {
        return ResponseEntity.ok(this.checkitemService.insert(checkitem));
    }

    /**
     * 编辑数据
     *
     * @param checkitem 实体
     * @return 编辑结果
     */
    @PutMapping
    @Operation(summary = "编辑检查项", description = "更新检查项信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "400", description = "更新失败")
    })
    public ResponseEntity<Checkitem> edit(@RequestBody @Parameter(description = "检查项对象") Checkitem checkitem) {
        return ResponseEntity.ok(this.checkitemService.update(checkitem));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    @Operation(summary = "删除检查项", description = "根据检查项ID删除检查项")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "400", description = "删除失败")
    })
    public ResponseEntity<Boolean> deleteById(@Parameter(description = "检查项ID") Long id) {
        return ResponseEntity.ok(this.checkitemService.deleteById(id));
    }
}